package ru.kruzhki.server.command;

import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import ru.kruzhki.shared.command.AddCompanyRequest;
import ru.kruzhki.shared.command.AddCompanyResponse;
import ru.kruzhki.shared.model.Company;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.googlecode.objectify.Key;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;


public class AddCompanyActionHandler implements ActionHandler<AddCompanyRequest, AddCompanyResponse> {

	private final ServletContext servletContext;

	private final Provider<HttpServletRequest> requestProvider;

	@Inject
	public AddCompanyActionHandler(final ServletContext servletContext, final Provider<HttpServletRequest> requestProvider) {
		this.servletContext = servletContext;
		this.requestProvider = requestProvider;
	}

	@Override
	public AddCompanyResponse execute(AddCompanyRequest request,	ExecutionContext context) throws ActionException {
		Map<Key<Company>, Company> result = ofy().save().entities(request.getCompany()).now();
		System.out.println(result);
		return new AddCompanyResponse(request.getCompany().getId());
	}

	@Override
	public void undo(AddCompanyRequest action, AddCompanyResponse result, ExecutionContext context) throws ActionException {
		//
	}

	@Override
	public Class<AddCompanyRequest> getActionType() {
		return AddCompanyRequest.class;
	}
}
